//
//  LCLoginVC.m
//  LongChain
//
//  Created by FengZi on 2018/3/19.
//  Copyright © 2018年 FengZi. All rights reserved.
//

#import "LCLoginVC.h"
#import "LCLoginModel.h"
#import "LCUserModel.h"
@interface LCLoginVC ()<UITextFieldDelegate>


@property (weak, nonatomic) IBOutlet UITextField *userPhone;
@property (weak, nonatomic) IBOutlet UITextField *userPassword;

@property (weak, nonatomic) IBOutlet UIButton *login;

@end

@implementation LCLoginVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self customTranslucentNavigationBar];
    
    
    if ([[NSUserDefaults standardUserDefaults]objectForKey:@"token"]) {
        NSString *token = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"token"]];
        LCLog(@"token = %@",token);
        [LCUserModel userModelManager].userid = token;
        
        UIViewController *loginVC = [self.storyboard instantiateViewControllerWithIdentifier:@"LCTabBarVC"];
        
        [UIApplication sharedApplication].keyWindow.rootViewController = loginVC;
    }
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textDidEditing:) name:UITextFieldTextDidChangeNotification object:nil];


}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.showLeftBarButtonItem = NO;
    UIImageView *bgImage = [[UIImageView alloc]initWithFrame:self.view.frame];
    bgImage.image = [UIImage imageNamed:@"loginBackground"];
    [self.view addSubview:bgImage];
    [self.view insertSubview:bgImage atIndex:0];
}

#pragma mark - - Action
- (void)textDidEditing:(UITextField *)textField {
    
    if (_userPhone.text.length >= 11) {
        NSString *currentPhone = _userPhone.text;
        self.userPhone.text = [currentPhone substringWithRange:NSMakeRange(0, 11)];

        [_userPhone resignFirstResponder];
        [_userPassword becomeFirstResponder];
    }
    if (_userPhone.text.length == 11 && _userPassword.text.length > 6) {
        _login.userInteractionEnabled = YES;
        _login.backgroundColor = [UIColor whiteColor];
        UIColor *titleColor = [UIColor colorWithRed:80/255.0 green:149/255.0 blue:244/255.0 alpha:1];
        [_login setTitleColor:titleColor forState:UIControlStateNormal];
        [_login setTitleColor:titleColor forState:UIControlStateSelected];

    }else{
        _login.userInteractionEnabled = NO;
        UIColor *backgroundColor = RGBAlpha(255.0f, 255.0f, 255.0f, 0.3);
        _login.backgroundColor = backgroundColor;
        UIColor *titleColor = RGBAlpha(255.0f, 255.0f, 255.0f, 0.6);
        [_login setTitleColor:titleColor forState:UIControlStateNormal];
        [_login setTitleColor:titleColor forState:UIControlStateSelected];

        
    }
}
- (IBAction)changePasswordSecture:(UIButton *)sender {
    NSString *imageName = (self.userPassword.secureTextEntry) ? @"show" : @"hiddenOrShow";
    [sender setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [sender setImage:[UIImage imageNamed:imageName] forState:UIControlStateSelected];
    self.userPassword.secureTextEntry = !self.userPassword.secureTextEntry;

}

- (IBAction)loginAction:(id)sender {
    NSLog(@"登录");
    if (self.userPhone.text.length > 0 && self.userPassword.text.length > 0 ) {
        
        NSString *temp = @"e&VW$4#@Fk9)UC6b";
        LCLog(@"%@",temp);
        NSString *p1 = [[LCEncryptionManager encryptionManager]lc_MD5EncryptionWithOriginData:temp];
        LCLog(@"%@",p1);
        
        NSString *p2 = [[LCEncryptionManager encryptionManager]lc_MD5EncryptionWithOriginData:self.userPassword.text];
        LCLog(@"%@",p2);
        NSString *origin = [NSString stringWithFormat:@"%@%@",p1,p2];
        LCLog(@"%@",origin);
        NSString *password = [[LCEncryptionManager encryptionManager]lc_MD5EncryptionWithOriginData:origin];
        LCLog(@"%@",password);
        
       
        NSDictionary *loginDic = @{@"account":self.userPhone.text,@"password":password};
        
        
        [[LCNetwork shareInstance]lc_postNetworkWithRUL:@"/api/v0/login" parameter:loginDic responseSuccess:^(id resultCode, id resultParameter) {
            LCLog(@"登录 返回 = %@",resultParameter);
            if ([resultCode isEqualToString:@"0"]) {
                
//                NSDictionary *data = resultParameter[@"data"];
//                NSString *token = [NSString stringWithFormat:@"%@",data[@"token"]];
//                [UserDefault setObject:token forKey:@"token"];
//                NSDictionary *userDic = data[@"user"];
                NSString *phone = [NSString stringWithFormat:@"%@",resultParameter[@"phone"]];
                [UserDefault setObject:phone forKey:@"phone"];
                [UserDefault synchronize];
                NSString *userid = [NSString stringWithFormat:@"%@",resultParameter[@"userid"]];
                [LCUserModel userModelManager].userid = userid;
                [LCUserModel userModelManager].mobile = phone;
                NSMutableData *data = [[NSMutableData alloc]init];
                NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
                [archiver encodeObject:[LCUserModel userModelManager] forKey:@"userModel"];
                [archiver finishEncoding];
                NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *fileName = [array.firstObject stringByAppendingPathComponent:@"userModel"];
                if ([data writeToFile:fileName atomically:YES]) {
                    LCLog(@"存储成功");
                }
                
                UITabBarController *tabBarVC = [self.storyboard instantiateViewControllerWithIdentifier:@"LCTabBarVC"];
                [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVC;
                
            }else{
                [self showHint:resultParameter];
            }
        } responseFail:^(id resultCode, NSError *error) {
            LCLog(@"登录 返回 = %@",error);
            
        }];

    }
    
    
    
}

- (IBAction)forgetPasswordAction:(UIButton *)sender {

    NSLog(@"忘记密码");
}

- (IBAction)registAction:(UIButton *)sender {
    
    NSLog(@"注册");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
